package middleware

import (
	"bsm/models"

	"github.com/gin-gonic/gin"
)

// 上下文键名常量
const (
	UserIDKey       = "user_id"
	UserNameKey     = "user_name"
	UserOrgKey      = "org_code"
	UserOrgLevelKey = "org_level"
	UserTypeKey     = "user_type"
)

// SetUserContext 设置用户上下文信息
func SetUserContext(c *gin.Context, user *models.WebUser) {
	c.Set(UserIDKey, user.UserID)
	c.Set(UserNameKey, user.UserName)
	c.Set(UserOrgKey, user.OrgCode)
	c.Set(UserOrgLevelKey, user.OrgLevel)
	c.Set(UserTypeKey, user.UserType)
}

// GetUserIDFromContext 从上下文获取用户ID
func GetUserIDFromContext(c *gin.Context) (string, bool) {
	userID, exists := c.Get(UserIDKey)
	if !exists {
		return "", false
	}
	return userID.(string), true
}

// MustGetUserID 必须获取用户ID（失败则panic）
func MustGetUserID(c *gin.Context) string {
	userID, exists := c.Get(UserIDKey)
	if !exists {
		panic("user_id not found in context")
	}
	return userID.(string)
}
